package com.openway.game.view.impl;

import com.openway.game.Game;
import com.openway.game.personage.GameObject;
import com.openway.game.view.AbstractPseudoGraphView;

import java.io.FileWriter;
import java.io.IOException;

/**
 * Pseudo-graph view implementation that write the state of the game to the output file.
 *
 * @author Created: 29.02.12 15:29
 * @version $Date$ $Revision$
 */
public class PseudoGraphFileView extends AbstractPseudoGraphView {

    private String fileName = "output.txt";

    public PseudoGraphFileView() {
    }

    public PseudoGraphFileView(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void render() throws IOException {
        FileWriter writer = null;
        try {
            writer = new FileWriter(fileName, false);
            writer.write(getDescription());
            writer.write('\n');
            for (GameObject[] row : Game.getInstance().getPlayingField().getFieldSnapshot()) {
                for (GameObject gameObject : row) {
                    writer.write(getObjectSymbol(gameObject) + " ");
                }
                writer.write('\n');
            }
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
}
